package com.wwj.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.springboot.entity.CmfzAdmin;
import com.wwj.springboot.mapper.CmfzAdminMapper;
import com.wwj.springboot.service.CmfzAdminService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author wwj
 */
@Service
@Transactional
public class CmfzAdminServiceImpl extends ServiceImpl<CmfzAdminMapper, CmfzAdmin> implements CmfzAdminService {

    @Autowired
    private CmfzAdminMapper cam;






    @Override
    public Map addAdmin(CmfzAdmin cmfzAdmin) {
        Map map = new HashMap();
        CmfzAdmin cmfz =  selectByName(cmfzAdmin.getUsername());
        System.out.println(cmfz);
        if (cmfz==null){
            try {
                String salt = UUID.randomUUID().toString().substring(0, 4);

                System.out.println(salt);

                Md5Hash md5Hash = new Md5Hash(cmfzAdmin.getPassword(), salt, 1024);
                cmfzAdmin.setPassword(md5Hash.toHex());
                cmfzAdmin.setSalt(salt);
                int i = cam.insert(cmfzAdmin);
                map.put("addAdmin","注册成功");
            } catch (Exception exception) {
                map.put("addAdmin","注册失败");
                exception.printStackTrace();
            }
        }else {
            map.put("addAdmin","用已存在");
        }
        return map;
    }

    @Override
    @Transactional(readOnly = true)
    public CmfzAdmin selectOne(int id) {
        return cam.selectById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public CmfzAdmin selectByName(String username) {
        QueryWrapper<CmfzAdmin> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        return cam.selectOne(wrapper);
    }

    @Override
    public Map updateAdmin(CmfzAdmin admin) {
        Map map = new HashMap();
        try {
            int i = cam.updateById(admin);
            map.put("update",true);
        } catch (Exception exception) {
            map.put("update",false);
            exception.printStackTrace();
        }
        return map;
    }

    @Override
    public Map deleteAdmin(int id) {
        Map map = new HashMap();
        try {
            int i = cam.deleteById(id);
            map.put("delete",true);
        } catch (Exception exception) {
            map.put("delete",false);
            exception.printStackTrace();
        }
        return map;
    }

    @Override
    public  Map selectByPage(int page, int limit, String username){

        int count = cam.selectCounts(username);
        int offset=(page-1)*limit;
        List<CmfzAdmin> list = cam.selectByPage(offset,limit,username);
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",list);
        return map;
    }
}
